package cn.topcodes.tcsf.awi.service;

import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.app.domain.entity.AppUser;
import cn.topcodes.tcsf.app.service.AppUserService;
import cn.topcodes.tcsf.awi.domain.entity.AppWechatUserMapping;
import cn.topcodes.tcsf.wechat.domain.entity.WechatUser;
import cn.topcodes.tcsf.wechat.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

/**
 * @author unicorn
 */
@Service
public class AwiServiceImpl extends GenericService<AppWechatUserMapping> implements AwiService {

    @Autowired
    private WechatUserService wechatUserService;

    @Autowired
    private AppUserService appUserService;

    /**
     * app和微信账号绑定
     * @param appUserId     app用户ID
     * @param wechatUserId  微信用户ID
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void bind(Long appUserId, Long wechatUserId) {
        Assert.isTrue(this.appUserService.exist(appUserId), "app用户不存在");
        Assert.isTrue(this.wechatUserService.exist(wechatUserId), "微信用户不存在");
        Example example = this.buildExample();
        example.or().andEqualTo(AppWechatUserMapping.FIELD_APP_USER_ID, appUserId);
        example.or().andEqualTo(AppWechatUserMapping.FIELD_WECHAT_USER_ID, wechatUserId);
        Assert.isTrue(!this.exist(example), "已被绑定");
        AppWechatUserMapping mapping = new AppWechatUserMapping();
        mapping.setAppUserId(appUserId);
        mapping.setWechatUserId(wechatUserId);
        this.create(mapping);
    }

    /**
     * app和微信账号解除绑定
     * @param wechatUserId  微信用户ID
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void unbindByWechatUserId(Long wechatUserId) {
        Example example = this.buildExample();
        example.or().andEqualTo(AppWechatUserMapping.FIELD_WECHAT_USER_ID, wechatUserId);
        this.deleteByExample(example);
    }

    /**
     * app和微信账号解除绑定
     * @param appUserId  APP用户ID
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void unbindByAppUserId(Long appUserId) {
        Example example = this.buildExample();
        example.or().andEqualTo(AppWechatUserMapping.FIELD_APP_USER_ID, appUserId);
        this.deleteByExample(example);
    }

    /**
     * 通过openId查找App用户
     *
     * @param openId 微信OpenId
     * @return App用户信息
     */
    @Override
    public AppUser findAppUserByOpenId(String openId) {
        WechatUser wechatUser = wechatUserService.findByOpenId(openId);
        if(wechatUser == null) {
            return null;
        }
        return this.findAppUserByWechatUserId(wechatUser.getId());
    }

    /**
     * 通过unionId查找App用户
     *
     * @param unionId 微信unionId
     * @return App用户信息
     */
    @Override
    public AppUser findAppUserByUnionId(String unionId) {
        WechatUser wechatUser = wechatUserService.findByUnionId(unionId);
        if(wechatUser == null) {
            return null;
        }
        return this.findAppUserByWechatUserId(wechatUser.getId());
    }

    /**
     * 通过微信用户ID获取app用户信息
     * @param wechatUserId  微信用户ID
     * @return  app用户信息
     */
    @Override
    public AppUser findAppUserByWechatUserId(Long wechatUserId) {
        Example example = this.buildExample();
        example.or().andEqualTo(AppWechatUserMapping.FIELD_WECHAT_USER_ID, wechatUserId);
        AppWechatUserMapping mapping = this.findOne(example);
        if(mapping == null) {
            return null;
        }
        return this.appUserService.findOne(mapping.getAppUserId());
    }

    /**
     * 通过App用户ID获取微信用户信息
     *
     * @param appUserId app用户ID
     * @return 微信用户信息
     */
    @Override
    public WechatUser findWechatUserByAppUserId(Long appUserId) {
        Example example = this.buildExample();
        example.or().andEqualTo(AppWechatUserMapping.FIELD_APP_USER_ID, appUserId);
        AppWechatUserMapping mapping = this.findOne(example);
        if(mapping == null) {
            return null;
        }
        return this.wechatUserService.findOne(mapping.getWechatUserId());
    }
}
